﻿using System;
using System.Web.Configuration;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Security.Wizard
{
    public partial class wizardCreateRoles : WebAdminUserControl
    {
        private string ApplicationPath
        {
            get { return (string) Session["WebAdminApplicationPath"]; }
        }

        public void AddRole(object sender, EventArgs e)
        {
            // REVIEW: Perf -too many switches in impersonation?
            var roleExists =
                (bool)
                ((WebAdminPage) Page).CallWebAdminHelperMethod(false, "RoleExists", new object[] {textBox1.Text},
                                                               new[] {typeof (string)});
            if (!textBox1.Text.Equals(string.Empty) && !roleExists)
            {
                try
                {
                    ((WebAdminPage) Page).CallWebAdminHelperMethod(false, "CreateRole", new object[] {textBox1.Text},
                                                                   new[] {typeof (string)});
                }
                catch
                {
                }

                BindGrid();
            }
        }

        private void BindGrid()
        {
            var roles =
                (string[]) ((WebAdminPage) Page).CallWebAdminHelperMethod(false, "GetAllRoles", new object[] {}, null);
            if (roles.Length > 0)
            {
                dataGrid.DataSource = roles;
                dataGrid.DataBind();
                dataGrid.Visible = true;
            }
            else
            {
                dataGrid.Visible = false;
            }
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            mv0.ActiveViewIndex = 0;
            ((WizardPage) Page).EnableWizardButtons();
        }

        public void DeleteRole(object sender, CommandEventArgs e)
        {
            mv0.ActiveViewIndex = 1;
            confirmation.DialogContent.Text = "<img src='../../Images/alert_lrg.gif'/>" +
                                              String.Format((string) GetLocalResourceObject("AreYouSure"),
                                                            e.CommandArgument);
                // "<img src='../../Images/alert_lrg.gif'/>Are you sure you want to delete the role <b>" + e.CommandArgument + "</b>?"; 
            Session["RoleName"] = e.CommandArgument;
            ((WizardPage) Page).DisableWizardButtons();
        }

        public string GetToolTip(string resourceName, string itemName)
        {
            var tempString = (string) GetLocalResourceObject(resourceName);
            return String.Format((string) GetGlobalResourceObject("GlobalResources", "ToolTipFormat"), tempString,
                                 itemName);
        }

        private void OK_Click(object sender, EventArgs e)
        {
            var roleName = (string) Session["RoleName"];

            if (
                (bool)
                ((WebAdminPage) Page).CallWebAdminHelperMethod(false, "RoleExists", new object[] {roleName},
                                                               new[] {typeof (string)}))
            {
                ((WebAdminPage) Page).CallWebAdminHelperMethod(false, "DeleteRole", new object[] {roleName, false},
                                                               new[] {typeof (string), typeof (bool)});
            }


            BindGrid();
            mv0.ActiveViewIndex = 0;
            ((WizardPage) Page).EnableWizardButtons();
        }

        public void IndexChanged(object sender, GridViewPageEventArgs e)
        {
            dataGrid.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        public void EnableRolesChanged(object sender, EventArgs e)
        {
            var cb = (CheckBox) sender;
            System.Configuration.Configuration config = ((WebAdminPage) Page).OpenWebConfiguration(ApplicationPath);
            var roleSection = (RoleManagerSection) config.GetSection("system.web/roleManager");
            roleSection.Enabled = cb.Checked;
            ((WebAdminPage) Page).SaveConfig(config);
        }

        public override bool OnNext()
        {
            if (!enableRoleCB.Checked)
            {
                return true;
            }
            int index = mv1.ActiveViewIndex;
            if (index == 1)
            {
                return true;
            }
            mv1.ActiveViewIndex = 1;
            return false;
        }

        public override bool OnPrevious()
        {
            if (mv1.ActiveViewIndex > 0)
            {
                mv1.ActiveViewIndex = mv1.ActiveViewIndex - 1;
                return false;
            }
            mv1.ActiveViewIndex = 0;
            return true;
        }

        public void Page_Init()
        {
            confirmation.DialogTitle.Text = (string) GetLocalResourceObject("DeleteRole");
            confirmation.LeftButton.Click += OK_Click;
            confirmation.RightButton.Click += Cancel_Click;
        }

        public void Page_Load()
        {
            bool enabled = ((WebAdminPage) Page).IsRoleManagerEnabled();
            if (!Page.IsPostBack)
            {
                enableRoleCB.Checked = enabled;
            }

            if (!enabled)
            {
                return;
            }

            // CONSIDER: Should not bind on each Load.
            var roles =
                (string[]) ((WebAdminPage) Page).CallWebAdminHelperMethod(false, "GetAllRoles", new object[] {}, null);
            int count = roles.Length;
            if (count > 0)
            {
                BindGrid();
            }
            else
            {
                dataGrid.Visible = false;
            }
        }
    }
}